package com.chy.order_meal.service;


import com.chy.order_meal.dao.UserDao;
import com.chy.order_meal.po.User;
import com.chy.order_meal.utility.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserDao dao;

    public Map<String,String> checkUser(String emaile,String phone){
        User u = dao.findFirstByEmailOrPhone(emaile,phone);
        if(u == null){
            return null;
        }
        Map result = new HashMap();
        String u_emaile = u.getEmail();
        if(emaile.equals(u_emaile)){
            result.put("code","1");
            result.put("msg","该邮箱已被注册");
            return result;
        }

        String u_phone = u.getPhone();
        if(phone.equals(u_phone)){
            result.put("code","2");
            result.put("msg","该电话号码已存在");
            return result;
        }
        return null;
    }


    public User save(User user) {
        return dao.save(user);
    }


    public Boolean login(String email, String phone, String password) {
        User u =null;
        if(Common.empty(email)){
            u = dao.findFirstByPhone(phone);
        }else{
            u = dao.findFirstByEmail(email);
        }
        return loginPasswordCheck(u,password);
    }


    public Boolean loginPasswordCheck(User u, String password) {
        if(u == null){
            return false;
        }
        String passwordOld = u.getPassword();
        if(password.equals(passwordOld)){
            return true;
        }else{
            return false;
        }
    }





}
